<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class My_feedback extends WxHomeController {

	function __construct() {
		parent::__construct();
		$this -> sidebar = "Student_feedback";
		$this -> load ->model("Student_feedback_model");
		$this -> load ->model("Supervisor_feedback_model");
		$this -> load ->model("Admin_model");
		$this -> load ->library("ProjectService/feedback_service");
		
	}
	function Index(){
		$data['id']=$_SESSION[SESS_USER_HOME]['id'];
		// $data['login_role']=$login_role;
		$this -> load ->view("home/my_feedback",$data);
	}
	function getData($id){
		$data=$_SESSION[SESS_USER_HOME]['role_id'].",".$_SESSION[SESS_USER_HOME]['sub_role_id'];
		$role=explode(",",$data);
		// foreach ($role as $k => $v) {
			if(in_array(WebRole::Student[XPHEnum::Value],$role ) || in_array(WebRole::StudentInfoOfficer[XPHEnum::Value],$role )||in_array(WebRole::UnitStudentInfoOfficer[XPHEnum::Value],$role )){
				$connar['items']="db_student_feedback.*";
				$connar['join']=array("db_student_feedback_record r","r.feedback_id=db_student_feedback.id");
				$connar['where']=$this->sqlEscape("db_student_feedback.creator=?? and del_flag=?? and r.creator=??",array($id,CommonStatus::UnDeleted[XPHEnum::Value],$id));
				$connar['orderby']="db_student_feedback.created desc,db_student_feedback.status asc";
				$result=$this -> Student_feedback_model ->get_list_full($connar);
				foreach ($result as $k => $v) {
					$result[$k]['created']= date('Y-m-d',$v['created']);
						//当前处理人
					if($v['belong']==0){
						$result[$k]['del_person']=$v['role_belong'];
						$result[$k]['del_person']=(new WebRole)->show($result[$k]['del_person']);
					}else{
						$result[$k]['del_person']=$v['belong'];
						$result[$k]['del_person']=(new WebRole)->show($result[$k]['del_person']);
					}
					
					$result[$k]['result']=$v['result']?$v['result']:'';
						//反馈状态
					$result[$k]['status']=(new FeedBackStatus)->show($v['status']);
				}
				exit(retJson("获取我的反馈",true,$result));
			}else if(in_array(WebRole::SchoolSupervisor[XPHEnum::Value],$role ) || in_array(WebRole::UnitSupervisorGroupLeader[XPHEnum::Value],$role )||in_array(WebRole::UnitSupervisor[XPHEnum::Value],$role ) ||in_array(WebRole::ExternalSupervisor[XPHEnum::Value],$role ) ||in_array(WebRole::Manage[XPHEnum::Value],$role ) ||in_array(WebRole::UnitSupervisorGroupLeader[XPHEnum::Value],$role )){
				$connar['items']="db_supervisor_feedback.*";
				$connar['join']=array("db_supervisor_feedback_record r","r.feedback_id=db_supervisor_feedback.id");
				$connar['where']=$this->sqlEscape("db_supervisor_feedback.creator=?? and del_flag=??  and r.creator=??",array($id,CommonStatus::UnDeleted[XPHEnum::Value],$id));
				$connar['orderby']="db_supervisor_feedback.created desc,db_supervisor_feedback.status asc";
				$result=$this -> Supervisor_feedback_model ->get_list_full($connar);
				foreach ($result as $k => $v) {
					$result[$k]['created']= date('Y-m-d',$v['created']);
								//当前处理人
					if($v['belong']==0){
						$result[$k]['del_person_id']=$v['role_belong'];
						$result[$k]['del_person']=(new WebRole)->show($result[$k]['del_person_id']);
					}else{
						$result[$k]['del_person_id']=$v['belong'];
						$result[$k]['del_person']=(new WebRole)->show($result[$k]['del_person_id']);
					}
								//反馈状态
					$result[$k]['status']=(new FeedBackStatus)->show($v['status']);
					$result[$k]['result']=$v['result']?$v['result']:'';
				}
				exit(retJson("获取我的反馈",true,$result));
			}
		// }
		
		// }
	}
	function detail(){
		$login_role=$_POST['login_role'];
		$id=$_POST['id'];
		$connar=array();
		// if($login_role==WebRole::SchoolSupervisor[XPHEnum::Value]||$login_role==WebRole::UnitSupervisorGroupLeader[XPHEnum::Value]||$login_role==WebRole::UnitSupervisor[XPHEnum::Value]||$login_role==WebRole::ExternalSupervisor[XPHEnum::Value]){
		$connar['where']=$this->sqlEscape("db_supervisor_feedback.id=?? ",array($id));
		$result=$this -> Supervisor_feedback_model ->get_list_full($connar);
		foreach ($result as $k => $v) {
			$result[$k]['created']= date('Y-m-d',$v['created']);
				//当前处理人
			if($v['belong']==0){
				$result[$k]['del_person']=$v['role_belong'];
				$result[$k]['del_person']=(new WebRole)->show($result[$k]['del_person']);
			}else{
				$result[$k]['del_person']=$v['belong'];
				$result[$k]['del_person']=(new WebRole)->show($result[$k]['del_person']);
			}
				//反馈状态
			$result[$k]['status']=(new FeedBackStatus)->show($v['status']);				
		}
		exit(retJson("获取我的反馈",true,$result));
		// }else if($login_role==WebRole::Student[XPHEnum::Value]||$login_role==WebRole::StudentInfoOfficer[XPHEnum::Value]){
		// 	exit(retJson("获取我的反馈",true,$result));
		// }

	}

	function delete(){
		$feedback_id=$_POST['feedback_id'];
		$data=$_SESSION[SESS_USER_HOME]['role_id'].",".$_SESSION[SESS_USER_HOME]['sub_role_id'];
		$role=explode(",",$data);
		foreach ($role as $k => $v) {
			if($v==WebRole::Student[XPHEnum::Value]||$v==WebRole::StudentInfoOfficer[XPHEnum::Value] ||$v==WebRole::UnitStudentInfoOfficer[XPHEnum::Value]){
				$result = $this -> Student_feedback_model -> get_single(array("id" => $feedback_id));
				if($result['status']==FeedBackStatus::HaveWrite[XPHEnum::Value]){
					exit(retJson("该反馈已在处理中，无法删除",false));
				}
				$this -> Student_feedback_model->update(array("id"=>$feedback_id,"del_flag"=>CommonStatus::Deleted[XPHEnum::Value]));
				exit(retJson("删除成功！",true));
			}else if($v==WebRole::SchoolSupervisor[XPHEnum::Value]||$v==WebRole::UnitSupervisorGroupLeader[XPHEnum::Value]||$v==WebRole::UnitSupervisor[XPHEnum::Value]||$v==WebRole::ExternalSupervisor[XPHEnum::Value] ||$v==WebRole::Manage[XPHEnum::Value]){
				$result = $this -> Supervisor_feedback_model -> get_single(array("id" => $feedback_id));
				if($result['status']==FeedBackStatus::HaveWrite[XPHEnum::Value]){
					exit(retJson("该反馈已在处理中，无法删除"));
				}
				$this -> Supervisor_feedback_model->update(array("id"=>$feedback_id,"del_flag"=>CommonStatus::Deleted[XPHEnum::Value]));
				exit(retJson("删除成功！",true));
			}
		}
	}

	function gotoDetail($id){
		$role=explode(',',$_SESSION[SESS_USER_HOME]['sub_role_id']);
		array_push($role, $_SESSION[SESS_USER_HOME]['role_id']);
		if(in_array(WebRole::StudentInfoOfficer[XPHEnum::Value], $role) || in_array(WebRole::UnitStudentInfoOfficer[XPHEnum::Value], $role)){
			$feedback_model='Student_feedback_model';
			$feedback=FeedbackType::Student[XPHEnum::Value];
		}else if(in_array(WebRole::SchoolSupervisor[XPHEnum::Value], $role) || in_array(WebRole::UnitSupervisor[XPHEnum::Value], $role) || in_array(WebRole::Manage[XPHEnum::Value], $role) || in_array(WebRole::UnitSupervisorGroupLeader[XPHEnum::Value], $role)){
			$feedback_model='Supervisor_feedback_model';
			$feedback=FeedbackType::Supervisor[XPHEnum::Value];
		}
		$data['detail']=$this->$feedback_model->get_single(array('id'=>$id));
		$data['detail']['result']=$data['detail']['result']?$data['detail']['result']:'';
		$data['detail']['created']= date('Y-m-d',$data['detail']['created']);

		//当前处理人
		if($data['detail']['belong']==0){
			$data['detail']['del_person_id']=$data['detail']['role_belong'];
			$data['detail']['del_person']=(new WebRole)->show($data['detail']['del_person_id']);
		}else{
			$data['detail']['del_person_id']=$data['detail']['belong'];
			$data['detail']['del_person']=(new WebRole)->show($data['detail']['del_person_id']);
		}
		$this->load->library('ProjectService/feedback_service');
		$data['img']=$this->feedback_service->getFeedBackImg($feedback,$id);
		//更新阅读状态
		$this->$feedback_model->update(array('is_view'=>0,'id'=>$id),'id');
		$this->load->view('home/student_feedback',$data);
	}

	// 反馈管理
	function Manage(){
		$data['id']=$_SESSION[SESS_USER_HOME]['id'];
		// $data['login_role']=$login_role;
		$this -> load ->view("home/feedback_manage",$data);
	}

	// 学生反馈管理
	function Student(){
		$data['id']=$_SESSION[SESS_USER_HOME]['id'];
		// $data['login_role']=$login_role;
		$this -> load ->view("home/feedback_student",$data);
	}

	function gotoDetail1($type,$id){
		$role=explode(',',$_SESSION[SESS_USER_HOME]['sub_role_id']);
		array_push($role, $_SESSION[SESS_USER_HOME]['role_id']);
		// if(in_array(WebRole::StudentInfoOfficer[XPHEnum::Value], $role) || in_array(WebRole::UnitStudentInfoOfficer[XPHEnum::Value], $role)){
		// 	$feedback_model='Student_feedback_model';
		// 	$feedback=FeedbackType::Student[XPHEnum::Value];
		// }else if(in_array(WebRole::SchoolSupervisor[XPHEnum::Value], $role) || in_array(WebRole::UnitSupervisor[XPHEnum::Value], $role)){
		// 	$feedback_model='Supervisor_feedback_model';
		// 	$feedback=FeedbackType::Supervisor[XPHEnum::Value];
		// }
		// $data['detail']=$this->$feedback_model->get_single(array('id'=>$id));
		// var_dump((new FeedbackType)->showByIndex($type,2));
		$model=$type.'_feedback_model';
		$connar['items']="db_admin.username,db_".(new FeedbackType)->showByIndex($type,2).'_feedback.*';
		$connar['join']=array('db_admin','db_admin.id=db_'.(new FeedbackType)->showByIndex($type,2).'_feedback.belong');
		$connar['where']=$this->sqlEscape('db_'.(new FeedbackType)->showByIndex($type,2).'_feedback.id=??',array($id));
		$data['detail']=$this->$model->get_single_full($connar);
		if(!$data['detail']['result']){
			$data['detail']['result']="无";
		}
		$this->load->library('ProjectService/feedback_service');
		$data['img']=$this->feedback_service->getFeedBackImg($type,$id);
		$data['type']=$type;
		 if(in_array(WebRole::StudentInfoOfficer[XPHEnum::Value], $role)){
			$data['detail']['is_studentInfoOfficer'] = CommonWhether::Whether[XPHEnum::Value];
		 }else{
		 	$data['detail']['is_studentInfoOfficer'] = CommonWhether::Not[XPHEnum::Value];
		 }
		 //查询提交人
	    $admin = $this->Admin_model->get_single_full(array('items'=>'username','where'=>'id='.$data['detail']['creator']));
	 	$data['detail']['creator']=$admin['username'];
	 	
		$this->load->view('home/feedback_deal',$data);
	}

	// 获取反馈查看身份
	function checkRole(){
		$role=explode(',',$_SESSION[SESS_USER_HOME]['sub_role_id']);
		array_push($role, $_SESSION[SESS_USER_HOME]['role_id']);
		if(in_array(WebRole::UnitSupervisorGroupLeader[XPHEnum::Value], $role) && in_array(WebRole::Manage[XPHEnum::Value], $role)){
			$data=array(0=>array(WebRole::Manage[XPHEnum::Value],WebRole::Manage[XPHEnum::Caption]),
								1=>array(WebRole::UnitSupervisorGroupLeader[XPHEnum::Value],WebRole::UnitSupervisorGroupLeader[XPHEnum::Caption]));	
		}else if(in_array(WebRole::UnitSupervisorGroupLeader[XPHEnum::Value], $role)){
			$data=array(0=>array(WebRole::UnitSupervisorGroupLeader[XPHEnum::Value],WebRole::UnitSupervisorGroupLeader[XPHEnum::Caption]));
		}else if(in_array(WebRole::Manage[XPHEnum::Value], $role)){
			$data=array(0=>array(WebRole::Manage[XPHEnum::Value],WebRole::Manage[XPHEnum::Caption]));
		}else{
			$data=array();
		}
		exit(retJson('返回反馈身份',true,$data));
	}

	function getDatas($feedback_type,$role){
		$model=$feedback_type.'_feedback_model';
		$table='db_'.(new FeedbackType)->showByIndex($feedback_type,XPHEnum::Remark).'_feedback';
		$connar = array();
		
		$role_id=explode(',',$_SESSION[SESS_USER_HOME]['sub_role_id']);
		array_push($role_id,$_SESSION[SESS_USER_HOME]['role_id']);
		if(in_array(WebRole::Manage[XPHEnum::Value],$role_id)){
			$connar['orderby'] = " role_belong desc,status asc,id desc";
		}else{
			$connar['orderby'] = "status asc,id desc";
		}
		
		$connar['join'][0]=array('admin ac','ac.id='.$table.'.creator');
		$connar['join'][1]=array('admin ab','ab.id='.$table.'.belong');
		$connar['where']=' del_flag= '.CommonStatus::UnDeleted[XPHEnum::Value];
		//根据当前role_belong的可见范围
		$admin=$this->Admin_model->get_single(array('id'=>$_SESSION[SESS_USER_HOME]['id']));

		// $connar['where'].=$this->sqlEscape(" and (find_in_set(".$table.".role_belong,??) or ".$table.".belong=??) ",array($admin['role_id'].$admin['sub_role_id'],$admin['id']));
		//不同角色页面
		if($role==WebRole::Teacher[XPHEnum::Value]){
			$connar['where'].=$this->sqlEscape(" and ".$table.".belong=?? ",array($admin['id']));
		}
		else if($role==WebRole::Manage[XPHEnum::Value]){
			// $connar['where'].=$this->sqlEscape(" and ".$table.".role_belong=?? ",array(WebRole::Manage[XPHEnum::Value]));
		}
		else if($role==WebRole::UnitSupervisorGroupLeader[XPHEnum::Value]){//督导组长
			$connar['where'].=$this->sqlEscape(" and ".$table.".role_belong=?? ",array(WebRole::UnitSupervisorGroupLeader[XPHEnum::Value]));
			$connar['where'].=$this->sqlEscape(" and ac.dept_name=?? ",array($admin['dept_name']));
		}
		else if($role==WebRole::SupervisorDirector[XPHEnum::Value]){
			$connar['where'].=$this->sqlEscape(" and (".$table.".status=?? or ".$table.".status=?? ) ",array(FeedBackStatus::HaveWrite[XPHEnum::Value],FeedBackStatus::End[XPHEnum::Value]));
		}
		else if($role==WebRole::SchoolLeader[XPHEnum::Value]){
			$connar['where'].=$this->sqlEscape(" and ".$table.".show=?? ",array(CommonWhether::Whether[XPHEnum::Value]));
		}
		else if($role==WebRole::StudentInfoOfficer[XPHEnum::Value]){//校级信息员
			$connar['where'].=$this->sqlEscape(" and ".$table.".dept_name=?? ",array($admin['dept_name']));
		}
		else{
			$connar['where'].=" 1=-1 ";
		}
		$connar['items']=$table.".*,ac.username creator_char,ifnull(ab.username,'--') belong_char ";
		$data = $this -> $model -> get_list_full($connar);
		if($feedback_type==FeedbackType::Supervisor[XPHEnum::Value]){
			$feedbackType='SupervisorFeedbackType';
		}else if($feedback_type==FeedbackType::Student[XPHEnum::Value]){
			$feedbackType='StudentFeedbackType';
		}
		foreach ($data as $k => $v) {
			$data[$k]['created']=date('Y-m-d H:i:s',$v['created']);

			$data[$k]['type']=(new $feedbackType)->show($v['type']).(new FeedbackType)->show($feedback_type);
			$data[$k]['status']=(new FeedBackStatus)->show($v['status']);
		}
		exit(retJson('返回反馈管理列表',true,$data));
	}

	// 反馈保存
	function writeResult($feedback_type,$id){
		$result=$this->feedback_service->writeResult($feedback_type,$id,$_POST['result'],$_SESSION[SESS_USER_HOME]['id']);
		exit($result);
	}

	// 反馈结束
	function doEnd($feedback_type,$id){
		$result=$this->feedback_service->endFeedBack($feedback_type,$id,true,$_SESSION[SESS_USER_HOME]['id']);
		exit($result);
	}
	// 转发人员列表
	function getAdminList($feedback_type,$id){
		$model=$feedback_type.'_feedback_model';
		$record_model=$feedback_type.'_feedback_record_model';
		$now=time();

		$feedback=$this->feedback_service->getFeedBack($feedback_type,$id);
		if(!$feedback){
			return array();
		}
		$connar['where']=$this->sqlLikeEscape("username like '%??%' or usercode like '%??%'",array($_POST['param'],$_POST['param']));
		if($feedback['role_belong']==WebRole::Manage[XPHEnum::Value]){
			$admin=$this->Admin_model->get_list_full($connar);
		}
		else if($feedback['role_belong']==WebRole::UnitSupervisorGroupLeader[XPHEnum::Value]){
			$connar['where'].=$this->sqlEscape(" and dept_id=??",array($_SESSION[SESS_USER_HOME]['dept_id']));
			$admin=$this->Admin_model->get_list_full($connar);
		}
		foreach ($admin as $k => $v) {
			if($v['id']==$_SESSION[SESS_USER_HOME]['id']){
				unset($admin[$k]);
			}
		}
		exit(retJson('用户列表',true,$admin));
	}

	// 转发
	function doForward($feedback_type,$id){
		$result=$this->feedback_service->assignDealAdmin($feedback_type,$id,$_POST['belong'],true);
		exit($result);
	}

	// 获取快捷回复数据
	function getReplyList($type)
	{
		$this->load->model('Feedback_reply_config_model');
		$result = $this -> Feedback_reply_config_model ->get_list(array("status"=>CommonWhether::Whether[XPHEnum::Value],'type'=>$type));
		exit(retJson("返回类型",true,$result));
	}
}